package com.jiangjiang.eshop.schedule.stock;

import com.google.common.collect.Sets;
import com.jiangjiang.eshop.order.dto.OrderItemDTO;
import com.jiangjiang.eshop.schedule.domain.GoodsAllocationStockEntity;
import com.jiangjiang.eshop.schedule.domain.GoodsStockEntity;
import com.jiangjiang.eshop.schedule.domain.ScheduleGoodsAllocationStockDetail;
import com.jiangjiang.eshop.schedule.dto.SaleDeliveryScheduleResult;
import com.jiangjiang.eshop.schedule.dto.ScheduleOrderPickingItemDTO;
import com.jiangjiang.eshop.schedule.dto.ScheduleOrderSendOutDetailDTO;
import com.jiangjiang.eshop.schedule.service.ScheduleGoodsAllocationStockDetailService;
import com.jiangjiang.eshop.schedule.service.ScheduleGoodsAllocationStockService;
import com.jiangjiang.eshop.schedule.service.ScheduleGoodsStockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @ClassName PayOrderScheduleStockUpdater
 * @Description TODO
 * @Author jhw
 * @Date 2021/8/31 7:34
 * @Version 1.0
 **/
@Component
@Scope("prototype")
public class PayOrderScheduleStockUpdater extends AbstractScheduleStockUpdater {

    private SaleDeliveryScheduleResult result;
    @Autowired
    private ScheduleGoodsStockService scheduleGoodsStockService;
    @Autowired
    private ScheduleGoodsAllocationStockService scheduleGoodsAllocationStockService;
    @Autowired
    private ScheduleGoodsAllocationStockDetailService scheduleGoodsAllocationStockDetailService;

    @Override
    protected void updateGoodsStock() throws Exception {
        OrderItemDTO orderItem = result.getOrderItem();
        GoodsStockEntity entity = scheduleGoodsStockService.queryBySkuIds(
                Sets.newHashSet(orderItem.getGoodsSkuId())).get(0);
        entity.setLockedStockQuantity(entity.getLockedStockQuantity() - orderItem.getPurchaseQuantity());
        entity.setOutputStockQuantity(entity.getOutputStockQuantity() + orderItem.getPurchaseQuantity());
        scheduleGoodsStockService.updateById(entity);
    }

    @Override
    protected void updateGoodsAllocationStock() throws Exception {
        for (ScheduleOrderPickingItemDTO pickingItem : result.getPickingItems()) {
            GoodsAllocationStockEntity entity = scheduleGoodsAllocationStockService.queryBySkuId(pickingItem.getGoodsAllocationId(), pickingItem.getGoodsSkuId());
            entity.setLockedStockQuantity(entity.getLockedStockQuantity() - pickingItem.getPickingCount());
            entity.setOutputStockQuantity(entity.getOutputStockQuantity() + pickingItem.getPickingCount());
            scheduleGoodsAllocationStockService.updateById(entity);
        }
    }

    @Override
    protected void updateGoodsAllocationStockDetail() throws Exception {
        Set<Long> detailId = result.getSendOutDetails().stream()
                .map(ScheduleOrderSendOutDetailDTO::getGoodsAllocationStockDetailId)
                .collect(Collectors.toSet());

        Map<Long, ScheduleGoodsAllocationStockDetail> detailMap = scheduleGoodsAllocationStockDetailService.queryByIds(detailId)
                .stream()
                .collect(Collectors.toMap(ScheduleGoodsAllocationStockDetail::getId, Function.identity()));

        List<ScheduleGoodsAllocationStockDetail> details = result.getSendOutDetails().stream().map(dto -> {
            ScheduleGoodsAllocationStockDetail detail = detailMap.get(dto.getGoodsAllocationStockDetailId());
            // 已使用数据 = 上架数量 - 当前数量 - 锁定数量
            detail.setLockedStockQuantity(detail.getLockedStockQuantity() - dto.getSendOutCount());
            return detail;
        }).collect(Collectors.toList());

        scheduleGoodsAllocationStockDetailService.updateBatchById(details);
    }

    @Override
    public void setParameter(Object parameter) {
        this.result = (SaleDeliveryScheduleResult) parameter;
    }
}
